

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ex02")
public class Ex2 extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7632788663041852202L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) {

		response.setContentType("text/html");
		PrintWriter saida;

		try {
			saida = response.getWriter();

			saida.println("<form action=\"ex02\" method=\"post\">");
			saida.println("Cadastrar Recado<br>");
			saida.println("<table>");
			saida.println("<tr>");
			saida.println("<td>NOME:</td>");
			saida.println("<td><input type=\"text\" name=\"nome\"></td>");
			saida.println("</tr>");
			saida.println("<tr>");
			saida.println("<td>RECADO:</td>");
			saida.println("<td><input type=\"text\" name=\"recado\"></td>");
			saida.println("</tr>");
			saida.println("<tr>");
			saida.println("<td colspan=\"2\"><input type=\"submit\"></td>");
			saida.println("</tr>");
			saida.println("</table>");
			saida.println("<br>");

			

			// saida.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {

		response.setContentType("text/html");
		PrintWriter saida;
		
		List<String> livroVisitas;
		
		String nome = request.getParameter("nome");
		String recado = request.getParameter("recado");
		
		if(getServletContext().getAttribute("livroVisitas")!=null){
			livroVisitas=(ArrayList<String>)getServletContext().getAttribute("livroVisitas");
			livroVisitas.add("Nome:" + nome + " Recado:" + recado);
		}else{
			livroVisitas=new ArrayList<String>();
			getServletContext().setAttribute("livroVisitas",livroVisitas);
			livroVisitas.add("Nome:" + nome + " Recado:" + recado);
		}
		
		

		
		try {
			saida = response.getWriter();
			
			saida.println("<form action=\"ex02\" method=\"post\">");
			saida.println("Cadastrar Recado<br>");
			saida.println("<table>");
			saida.println("<tr>");
			saida.println("<td>NOME:</td>");
			saida.println("<td><input type=\"text\" name=\"nome\"></td>");
			saida.println("</tr>");
			saida.println("<tr>");
			saida.println("<td>RECADO:</td>");
			saida.println("<td><input type=\"text\" name=\"recado\"></td>");
			saida.println("</tr>");
			saida.println("<tr>");
			saida.println("<td colspan=\"3\"><input type=\"submit\"></td>");
			saida.println("</tr>");
			saida.println("</table>");
			saida.println("<br>");

			saida.println("RECADOS<br><br>");
			saida.println("<table border=\"2\">");

			for (Iterator iterator2 = livroVisitas.iterator(); iterator2
					.hasNext();) {
				String string = (String) iterator2.next();
				saida.println("<tr>");
				saida.println("<td>" + string + "</td>");
				saida.println("</tr>");
				
			}
			saida.println("</table>");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
